<style scoped lang="scss">
@import url('index.scss');
</style>
<template>
  <div class="app-container">
    <!--新模版样式-->
    <div class="topsearch">
      <el-row>
        <el-col :span="22" class="searchcol">
          <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="地区名称" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入地区名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="拼音" prop="pinyin">
              <el-input v-model="queryParams.pinyin" placeholder="请输入拼音" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="首字母" prop="initials">
              <el-input v-model="queryParams.initials" placeholder="请输入首字母" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="状态" prop="enable">
              <el-input v-model="queryParams.enable" placeholder="请输入状态" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22" class="rightcol">
          <div class="newbtn" @click="handleAdd" v-hasPermi="['system:area:add']">
            <img src="@/assets/images/add.svg" alt="">
            <span>新建</span>
          </div>
          <div class="newbtn" v-if="single == false" @click="handleUpdate" v-hasPermi="['system:area:edit']">
            <img src="@/assets/images/edit.svg" alt=""></img>
            <span>编辑</span>
          </div>
          <div class="newbtn" v-if="multiple == false" @click="handleDelete" v-hasPermi="['system:area:remove']">
            <img src="@/assets/images/del.svg" alt=""></img>
            <span>删除</span>
          </div>
          <div class="newbtn" @click="handleExport" v-hasPermi="['system:area:export']">
            <img src="@/assets/images/export.svg" alt=""></img>
            <span>导出</span>
          </div>
          <div class="newbtn" @click="getList">
            <img src="@/assets/images/refresh.svg" alt=""></img>
            <span>刷新</span>
          </div>
        </el-col>
        <!-- <el-col :span="2">
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-col> -->
      </el-row>
    </div>
    <!--end-->
    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:area:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:area:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:area:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:area:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <div class="newtable">
      <el-table v-loading="loading" :data="areaList" @selection-change="handleSelectionChange"
        :cell-style="{ backgroundColor: '#f9f9f9' }">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" width="60" align="center" prop="id" />
        <el-table-column label="地区名称" align="center" prop="name" />
        <el-table-column label="地区编码" align="center" prop="code" />
        <el-table-column label="层级" align="center" prop="level" />
        <el-table-column label="拼音" align="center" prop="pinyin" />
        <el-table-column label="首字母" align="center" prop="initials" />
        <el-table-column label="状态" align="center" prop="enable">
          <template #default="scope">
            <el-tag v-if="scope.row.enable == 1" type="success">正常</el-tag>
            <el-tag v-else type="danger">停用</el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column label="地区编码" align="center" prop="pcodeId" /> -->
        <el-table-column label="描述" align="center" prop="description" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['system:area:edit']">修改</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
              v-hasPermi="['system:area:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>



    <!-- 添加或修改地区数据对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" class="newDialog">
      <el-form ref="areaRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="地区名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入地区名称" />
        </el-form-item>
        <el-form-item label="地区编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入地区编码" />
        </el-form-item>
        <el-form-item label="层级" prop="level">
          <el-input v-model="form.level" placeholder="请输入层级" />
        </el-form-item>
        <el-form-item label="拼音" prop="pinyin">
          <el-input v-model="form.pinyin" placeholder="请输入拼音" />
        </el-form-item>
        <el-form-item label="首字母" prop="initials">
          <el-input v-model="form.initials" placeholder="请输入首字母" />
        </el-form-item>
        <el-form-item label="状态" prop="enable">
          <!-- <el-input v-model="form.enable" placeholder="请输入状态" /> -->
          <el-radio-group v-model="form.enable">
            <el-radio v-for="dict in ha_normal_status" :key="dict.value" :value="dict.value"
              :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="地区编码" prop="pcodeId">
          <el-input v-model="form.pcodeId" placeholder="请输入地区编码" />
        </el-form-item> -->
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Area">
import { listArea, getArea, delArea, addArea, updateArea } from "@/api/system/area"

const { proxy } = getCurrentInstance()
// 使用字典
const { ha_normal_status } = proxy.useDict('ha_normal_status'); // 使用字典

const areaList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    code: null,
    level: null,
    pinyin: null,
    initials: null,
    enable: null,
    pcodeId: null,
    description: null,
  },
  rules: {
    name: [
      { required: true, message: "地区名称不能为空", trigger: "blur" }
    ],
    code: [
      { required: true, message: "地区编码不能为空", trigger: "blur" }
    ],
    level: [
      { required: true, message: "层级不能为空", trigger: "blur" }
    ],
    pinyin: [
      { required: true, message: "拼音不能为空", trigger: "blur" }
    ],
    initials: [
      { required: true, message: "首字母不能为空", trigger: "blur" }
    ],
    enable: [
      { required: true, message: "状态不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询地区数据列表 */
function getList() {
  loading.value = true
  listArea(queryParams.value).then(response => {
    areaList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    code: null,
    level: null,
    pinyin: null,
    initials: null,
    enable: '1',
    pcodeId: null,
    description: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  }
  proxy.resetForm("areaRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加地区数据"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getArea(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改地区数据"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["areaRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateArea(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addArea(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除地区数据编号为"' + _ids + '"的数据项？').then(function() {
    return delArea(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/area/export', {
    ...queryParams.value
  }, `area_${new Date().getTime()}.xlsx`)
}

getList()
</script>
